﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TruongVuWebsite.Admin
{
    public partial class CustomerManagement : System.Web.UI.Page
    {
        private TruongVu.Business.CustomerModel _customerModel = new TruongVu.Business.CustomerModel();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.CustomerGridView.DataSource = this._customerModel.GetAll();
            this.CustomerGridView.DataBind();
        }

        protected void CustomerGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            List<TruongVu.Data.Customer> customers = this.CustomerGridView.DataSource as List<TruongVu.Data.Customer>;
            if (customers != null && customers.Count > e.RowIndex)
            {
                this._customerModel.Delete(customers[e.RowIndex].ID);
                customers.RemoveAt(e.RowIndex);
                this.CustomerGridView.DataBind();
            }
        }

        protected void CustomerGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            List<TruongVu.Data.Customer> customers = this.CustomerGridView.DataSource as List<TruongVu.Data.Customer>;
            if (customers != null && customers.Count > e.NewEditIndex)
            {
                Response.Redirect("Customer.aspx?ID=" + customers[e.NewEditIndex].ID);
            }
            else
            {
                e.Cancel = true;
            }
        }
    }
}